package com.xqboss.web.controller.nft;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.xqboss.apps.domain.nft.ChainTemplate;
import com.xqboss.apps.domain.nft.NftAuthor;
import com.xqboss.apps.mapper.nft.ChainTemplateMapper;
import com.xqboss.apps.service.nft.NftAuthorService;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.AjaxResult;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.core.page.TableDataInfo;
import com.xqboss.common.enums.BusinessType;
import com.xqboss.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者Controller
 *
 * @author xqboss
 * @date 2023-08-03
 */
@Api(tags = {"作者管理"})
@RestController
@RequestMapping("/web/author")
public class WebNftAuthorController extends BaseController {
    @Autowired
    private NftAuthorService nftAuthorService;
    @Resource
    private ChainTemplateMapper chainTemplateMapper;

    /**
     * 查询作者列表
     */
    @PreAuthorize("@ss.hasPermi('web:author:list-page')")
    @ApiOperation("查询作者列表")
    @GetMapping("/list-page")
    public R<List<NftAuthor>> list(NftAuthor nftAuthor) {
        if (ObjUtil.isNotNull(nftAuthor) && StrUtil.isNotBlank(nftAuthor.getKeyword())) {
            nftAuthor.setName(nftAuthor.getKeyword());
        }
        startPage();
        List<NftAuthor> list = nftAuthorService.selectNftAuthorList(nftAuthor);
        return R.ok(list);
    }

    /**
     * 导出作者列表
     */
    @PreAuthorize("@ss.hasPermi('web:author:export')")
    @Log(title = "作者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NftAuthor nftAuthor) {
        List<NftAuthor> list = nftAuthorService.selectNftAuthorList(nftAuthor);
        ExcelUtil<NftAuthor> util = new ExcelUtil<NftAuthor>(NftAuthor.class);
        util.exportExcel(response, list, "作者数据");
    }

    /**
     * 获取作者详细信息
     */
    @PreAuthorize("@ss.hasPermi('web:author:get')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(nftAuthorService.selectNftAuthorById(id));
    }

    /**
     * 新增作者
     */
    @PreAuthorize("@ss.hasPermi('web:author:add')")
    @Log(title = "作者", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody NftAuthor nftAuthor) {
        return toAjax(nftAuthorService.insertNftAuthor(nftAuthor));
    }

    /**
     * 修改作者
     */
    @PreAuthorize("@ss.hasPermi('web:author:edit')")
    @Log(title = "作者", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody NftAuthor nftAuthor) {
        return toAjax(nftAuthorService.updateNftAuthor(nftAuthor));
    }

    /**
     * 删除作者
     */
    @PreAuthorize("@ss.hasPermi('web:author:delete')")
    @Log(title = "作者", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nftAuthorService.deleteNftAuthorByIds(ids));
    }


    /**
     * 模板
     */
    @PreAuthorize("@ss.hasPermi('web:author:chain-template')")
    @GetMapping("/chain-template")
    public R<List<ChainTemplate>> templateList() {
        List<ChainTemplate> result = chainTemplateMapper.selectList(null);
        return R.ok(result);
    }
}

